package com.mach.platform.sprider.dev;

import java.io.UnsupportedEncodingException;

public class ByteUtil  
{  
    public static byte[] getBytes(short data)  
    {  
        byte[] bytes = new byte[2];  
        bytes[0] = (byte) (data & 0xff);  
        bytes[1] = (byte) ((data & 0xff00) >> 8);  
        return bytes;  
    }  
  
    public static byte[] getBytes(char data)  
    {  
        byte[] bytes = new byte[2];  
        bytes[0] = (byte) (data);  
        bytes[1] = (byte) (data >> 8);  
        return bytes;  
    }  
  
    public static byte[] getBytes(int data)  
    {  
        byte[] bytes = new byte[4];  
        bytes[0] = (byte) (data & 0xff);  
        bytes[1] = (byte) ((data & 0xff00) >> 8);  
        bytes[2] = (byte) ((data & 0xff0000) >> 16);  
        bytes[3] = (byte) ((data & 0xff000000) >> 24);  
        return bytes;  
    }  
  
    public static byte[] getBytes(long data)  
    {  
        byte[] bytes = new byte[8];  
        bytes[0] = (byte) (data & 0xff);  
        bytes[1] = (byte) ((data >> 8) & 0xff);  
        bytes[2] = (byte) ((data >> 16) & 0xff);  
        bytes[3] = (byte) ((data >> 24) & 0xff);  
        bytes[4] = (byte) ((data >> 32) & 0xff);  
        bytes[5] = (byte) ((data >> 40) & 0xff);  
        bytes[6] = (byte) ((data >> 48) & 0xff);  
        bytes[7] = (byte) ((data >> 56) & 0xff);  
        return bytes;  
    }  
  
    public static byte[] getBytes(float data)  
    {  
        int intBits = Float.floatToIntBits(data);  
        return getBytes(intBits);  
    }  
  
    public static byte[] getBytes(double data)  
    {  
        long intBits = Double.doubleToLongBits(data);  
        return getBytes(intBits);  
    }  
  
    public static byte[] getBytes(String data, String charsetName) throws UnsupportedEncodingException  
    {  
        return data.getBytes(charsetName);  
    }  
  
    public static byte[] getBytes(String data) throws UnsupportedEncodingException  
    {  
        return getBytes(data, "GBK");  
    }  
  
      
    public static short getShort(byte[] bytes)  
    {  
        return (short) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));  
    }  
  
    public static char getChar(byte[] bytes)  
    {  
        return (char) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));  
    }  
  
    public static int getInt(byte[] bytes)  
    {  
        return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16)) | (0xff000000 & (bytes[3] << 24));  
    }
    public static int getInt2Byte(byte[] bytes)
    {
        return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8));
    }

    public static long getLong(byte[] bytes)  
    {  
        return(0xffL & (long)bytes[0]) | (0xff00L & ((long)bytes[1] << 8)) | (0xff0000L & ((long)bytes[2] << 16)) | (0xff000000L & ((long)bytes[3] << 24))  
         | (0xff00000000L & ((long)bytes[4] << 32)) | (0xff0000000000L & ((long)bytes[5] << 40)) | (0xff000000000000L & ((long)bytes[6] << 48)) | (0xff00000000000000L & ((long)bytes[7] << 56));  
    }  
    
    public static float getFloat(byte[] bytes) {
    	return getFloat(bytes,false);
    }
  
    public static float getFloat(byte[] bytes, boolean reve)  
    {  
    	if(reve) {
    		bytes = reverse(bytes);
    	}
        return Float.intBitsToFloat(getInt(bytes));  
    }
    public static float getFloat(byte[] bytes,int ix, int len, boolean reve) {
    	byte[] result = new byte[len];
    	
    	for(int i = 0; i < len; ++i) {
    		result[i] = bytes[ix + i];
    	}
    	
    	return getFloat(result, reve);
    }
    
    public static byte[] subData(byte[] bytes,int ix, int len) {
    	byte[] result = new byte[len];
    	
    	for(int i = 0; i < len; ++i) {
    		result[i] = bytes[ix + i];
    	}
    	
    	return result;
    }
  
    public static double getDouble(byte[] bytes)  
    {  
        long l = getLong(bytes);  
        System.out.println(l);  
        return Double.longBitsToDouble(l);  
    }  
  
    public static String getString(byte[] bytes, String charsetName) throws UnsupportedEncodingException  
    {  
        return new String(bytes, charsetName);  
    }  
  
    public static String getString(byte[] bytes) throws UnsupportedEncodingException  
    {  
        return getString(bytes, "GBK");  
    }  
  
    public static void main(String[] args) throws UnsupportedEncodingException  {
    	
    	byte[] data1 = {0,0,(byte)0xBF,(byte)0x41,(byte)0x80,(byte)0x44};
    	byte[] data4 = {0x20,0x43,0x00,0x00};
    	byte[] data5 =reverse(data1);
    	byte[] data6 = {0x40,0x2A,0x3D,0x71};
    	byte[] data7 = {0x41,0x76,(byte)0xAA,(byte)0xAB};
    	UU.printData(data5);
    	float f = getFloat(data5);
//    	System.out.println(getFloat(data5));
//    	System.out.println(getFloat(data1,2,4,false));
    	System.out.println(getFloat(data7));
    	System.out.println(getFloat(reverse(data7)));
    	
    	
//    	float ff = 10.0f;
//    	byte[] buf = getBytes(ff);
//    	U.printData(buf);
    	
//    	byte[] buf2 = {0x00,0x00,0x20,0x41};
//    	float f = getFloat(buf2);
//    	System.out.println(f);
    	
    }
    
    public static byte[] reverse(byte[] data) {
    	byte[] res = new byte[data.length];
    	
    	for(int i = 0; i < data.length; ++i) {
			byte d = data[i];
			res[data.length - i - 1] = d;
		}
    	
    	return res;
    }
    
    public static byte[] reverse2Byte(byte[] data) {
    	byte[] res = new byte[data.length];
    	
    	res[0] = data[2];
    	res[1] = data[3];
    	
    	res[2] = data[0];
    	res[3] = data[1];
    	
    	return res;
    }

    public static byte[] reveIn2Byte(byte[] data,int s) {
        if(data == null)return data;

        byte sV = data[s];
        byte eV = data[s+1];

        data[s] = eV;
        data[s+1] = sV;

        return data;

    }
      
    public static void test1(String[] args) throws UnsupportedEncodingException  
    {  
        short s = 122;  
        int i = 122;  
        long l = 1222222;  
  
        char c = 'a';  
  
        float f = 122.22f;  
        double d = 122.22;  
  
        String string = "���Ǻú���";  
        System.out.println(s);  
        System.out.println(i);  
        System.out.println(l);  
        System.out.println(c);  
        System.out.println(f);  
        System.out.println(d);  
        System.out.println(string);  
  
        System.out.println("**************");  
  
        System.out.println(getShort(getBytes(s)));  
        System.out.println(getInt(getBytes(i)));  
        System.out.println(getLong(getBytes(l)));  
        System.out.println(getChar(getBytes(c)));  
        System.out.println(getFloat(getBytes(f)));  
        System.out.println(getDouble(getBytes(d)));  
        System.out.println(getString(getBytes(string)));  
    }  
}  
